<?php

require_once './include/common.inc.php';
checkLogin();

$page_title = 'jans2en furniture - Wish List - Wish List Management';
getMyParam('pid');
getMyParam('wid');
getMyParam('act');
getMyParam('code');

$from_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $PHP_SELF;

if ($act == "add" && is_numeric($pid) && $pid>0) {
    $sql = "SELECT * FROM `product` WHERE `id` = $pid";
    $product_info_ary = $db->get_one($sql);
    
    $sql = "SELECT `id`, `product_list` FROM `wishlist` WHERE `user_id` = $jansen_uid AND `flag` = 0";
    $tmp = $db->get_one($sql);
    if (!empty($tmp)) {
        if (!in_array($pid, explode("|", $tmp['product_list']))) {
            $new_product_list = $tmp['product_list'] == "" ? $pid : $tmp['product_list'] . "|" . $pid;
            $sql = "UPDATE `wishlist` SET `product_list`= '$new_product_list' WHERE `id` = " . $tmp['id'];
            $db->query($sql);
        }
    } else {
        $sql = "INSERT INTO `wishlist` (product_list, date, user_id, flag) VALUES ($pid, $timestamp, $jansen_uid, 0)";
        $db->query($sql);
    }
    
    $sql = "SELECT `id` FROM `wishlist` WHERE `user_id` = $jansen_uid";
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);
    setcookie('jansen_wishlist_qty', $sql_all_num, $timestamp+3600*24*365, "/");

    echo "<script>alert('Item $code has been added to Wish List.');parent.location.href='" . $from_url . "';</script>";
    exit;
    //header("Location:wishlist_view.php");
} elseif ($act == "delall" && is_numeric($wid) && $wid>0) {
    $sql1 = "DELETE FROM `wishlist` WHERE `id` = $wid AND `user_id` = '$jansen_uid'";
    $sql2 = "DELETE FROM `sendrecord` WHERE `wishlist_id` = $wid AND `user_id` = '$jansen_uid'";
    $db->query($sql1);
    $db->query($sql2);
    header("Location:wishlist_view.php");
}


// *** page code start ***
$page_url = 'wishlist_view.php?wid=' . $wid . '&';
$sql = "SELECT `id` FROM `wishlist` WHERE `user_id` = $jansen_uid";
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);

$ppp = 10;
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}

$page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url, "wish lists");
// *** page code end ***

$sql = "SELECT `id`, `product_list`, `date` FROM `wishlist` WHERE `user_id` = $jansen_uid ORDER BY `date` DESC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $wishlist_ary[] = $rs;
}

//echo $sql_all_num;
setcookie('jansen_wishlist_qty', $sql_all_num, $timestamp+3600*24*365, "/");



$sql = "SELECT `id`, `wishlist_id`, `date`, `friend_name`, `friend_email` FROM `sendrecord` WHERE `user_id` = $jansen_uid";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $sendrecord_ary[$rs['wishlist_id']][] = $rs;
}

require_once PrintEot('header');
require_once PrintEot('wishlist_view');
require_once PrintEot('footer');

?>